package cn.iocoder.yudao.module.alipay.util;

import cn.iocoder.yudao.module.alipay.dal.dataobject.infoauthaliapp.InfoAuthAliAppDO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.*;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.thymeleaf.util.StringUtils;

import java.util.List;

public class Test {

    public static void main(String[] args) throws AlipayApiException {
        //小程序信息，logo，名称
        queryAppVersionList();

        //上传小程序版本
       //uploadAppVersion();

       //查询小程序版本
       // queryAppVersion();

        //删除小程序版本
        //delAppVersion();

        //提交审核
        //auditAppVersion();

        //撤回审核
       // cancleAppVersion();

        //上传版本
        //onlineAppVersion();

        //下架版本
       // offlineAppVersion();

    }

    public static void testAppInfo() throws AlipayApiException {
        CertAlipayRequest certAlipayRequest = Tool.getRequestTemplate();
        AlipayClient alipayClient = null;
        try {
            alipayClient = new DefaultAlipayClient(certAlipayRequest);
        } catch (AlipayApiException e1) {
            e1.printStackTrace();
        }

        // 构造请求参数以调用接口
        AlipayOpenMiniBaseinfoQueryRequest request = new AlipayOpenMiniBaseinfoQueryRequest();
        request.putOtherTextParam("app_auth_token", "202403BB0a199a29f36a494c88bee096f5d60X87");
        // 响应
        AlipayOpenMiniBaseinfoQueryResponse response = alipayClient.certificateExecute(request);
        System.out.println("response.getBody():");
        System.out.println(response.getBody());
        JSONObject jsonObject = JSON.parseObject(response.getBody());
        String alipayOpenMiniBaseinfoQueryResponse = jsonObject.getString("alipay_open_mini_baseinfo_query_response");
        JSONObject parse = JSON.parseObject(alipayOpenMiniBaseinfoQueryResponse);
        System.out.println(parse.getString("app_logo"));
        System.out.println(parse.getString("service_phone"));
    }


    public static void uploadAppVersion() throws AlipayApiException {
        CertAlipayRequest certAlipayRequest = Tool.getRequestTemplate();
        AlipayClient alipayClient = null;
        try {
            alipayClient = new DefaultAlipayClient(certAlipayRequest);
        } catch (AlipayApiException e1) {
            e1.printStackTrace();
        }
        // 构造请求参数以调用接口
        AlipayOpenMiniVersionUploadRequest request = new AlipayOpenMiniVersionUploadRequest();
        request.putOtherTextParam("app_auth_token", "202403BB0a199a29f36a494c88bee096f5d60X87");
        AlipayOpenMiniVersionUploadModel model = new AlipayOpenMiniVersionUploadModel();

        // 设置自定义参数
//        model.setExt("{\"extEnable\": true, \"extPages\": {\"pages/face/index\": {\"defaultTitle\": \"哈哈哈哈\"}},\"window\": {\"defaultTitle\": \"AI2\"}}");

        // 设置小程序模板版本号
        //model.setTemplateVersion("0.0.1");

        // 设置商家小程序版本号
        model.setAppVersion("1.0.1");

        // 设置小程序投放的端参数
        model.setBundleId("com.alipay.alipaywallet");

        // 设置小程序模板 APPID
        model.setTemplateId("2021004133687041");

        request.setBizModel(model);
        AlipayOpenMiniVersionUploadResponse response = alipayClient.certificateExecute(request);
        System.out.println("response.getBody():");
        System.out.println(response.getBody());
    }

    //查询小程序版本
    public static void queryAppVersion() throws AlipayApiException {
        CertAlipayRequest certAlipayRequest = Tool.getRequestTemplate();
        AlipayClient alipayClient = null;
        try {
            alipayClient = new DefaultAlipayClient(certAlipayRequest);
        } catch (AlipayApiException e1) {
            e1.printStackTrace();
        }

        // 构造请求参数以调用接口
        AlipayOpenMiniVersionBuildQueryRequest request = new AlipayOpenMiniVersionBuildQueryRequest();
        AlipayOpenMiniVersionBuildQueryModel model = new AlipayOpenMiniVersionBuildQueryModel();

        // 设置小程序版本号
        model.setAppVersion("1.0.1");

        // 设置小程序端
        model.setBundleId("com.alipay.alipaywallet");

        request.setBizModel(model);
        request.putOtherTextParam("app_auth_token", "202403BB0a199a29f36a494c88bee096f5d60X87");

        AlipayOpenMiniVersionBuildQueryResponse response = alipayClient.certificateExecute(request);
        //构建排队中: 0
        //正在构建: 1
        //构建成功: 2
        //构建失败: 3
        //构建超时: 5
        //版本创建成功: 6
        System.out.println(response.getBody());
    }


    //删除小程序版本
    public static void delAppVersion() throws AlipayApiException {
        CertAlipayRequest certAlipayRequest = Tool.getRequestTemplate();
        AlipayClient alipayClient = null;
        try {
            alipayClient = new DefaultAlipayClient(certAlipayRequest);
        } catch (AlipayApiException e1) {
            e1.printStackTrace();
        }

        // 构造请求参数以调用接口
        AlipayOpenMiniVersionDeleteRequest  request = new AlipayOpenMiniVersionDeleteRequest();
        AlipayOpenMiniVersionDeleteModel  model = new AlipayOpenMiniVersionDeleteModel();

        // 设置小程序版本号
        model.setAppVersion("1.0.1");

        // 设置小程序端
        model.setBundleId("com.alipay.alipaywallet");

        request.setBizModel(model);
        request.putOtherTextParam("app_auth_token", "202403BB0a199a29f36a494c88bee096f5d60X87");

        AlipayOpenMiniVersionDeleteResponse response = alipayClient.certificateExecute(request);
        System.out.println(response.getBody());
    }

    //提交审核
    public static void auditAppVersion() throws AlipayApiException {
        CertAlipayRequest certAlipayRequest = Tool.getRequestTemplate();
        AlipayClient alipayClient = null;
        try {
            alipayClient = new DefaultAlipayClient(certAlipayRequest);
        } catch (AlipayApiException e1) {
            e1.printStackTrace();
        }
        // 构造请求参数以调用接口
        AlipayOpenMiniVersionAuditApplyRequest request = new AlipayOpenMiniVersionAuditApplyRequest();
        request.putOtherTextParam("app_auth_token", "202403BB0a199a29f36a494c88bee096f5d60X87");

        // 设置审核类型
        request.setAuditRule("BASE_PROMOTE");

        // 设置小程序版本描述
        request.setVersionDesc("版本描述描述");

        // 设置小程序版本号
        request.setAppVersion("1.0.1");

        // 设置区域类型
        request.setRegionType("CHINA");

        // 设置是否加速
        request.setSpeedUp("true");

        AlipayOpenMiniVersionAuditApplyResponse response = alipayClient.certificateExecute(request);
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }

    //撤销审核
    public static void cancleAppVersion() throws AlipayApiException {
        CertAlipayRequest certAlipayRequest = Tool.getRequestTemplate();
        AlipayClient alipayClient = null;
        try {
            alipayClient = new DefaultAlipayClient(certAlipayRequest);
        } catch (AlipayApiException e1) {
            e1.printStackTrace();
        }
        // 构造请求参数以调用接口
        AlipayOpenMiniVersionAuditCancelRequest  request = new AlipayOpenMiniVersionAuditCancelRequest ();
        request.putOtherTextParam("app_auth_token", "202403BB0a199a29f36a494c88bee096f5d60X87");
        AlipayOpenMiniVersionAuditCancelModel model = new AlipayOpenMiniVersionAuditCancelModel();

        // 设置商家小程序审核中的版本号
        model.setAppVersion("1.0.1");

        // 设置小程序端
        model.setBundleId("com.alipay.alipaywallet");


        request.setBizModel(model);

        AlipayOpenMiniVersionAuditCancelResponse  response = alipayClient.certificateExecute(request);
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }

    //上传版本
    public static void onlineAppVersion() throws AlipayApiException {
        CertAlipayRequest certAlipayRequest = Tool.getRequestTemplate();
        AlipayClient alipayClient = null;
        try {
            alipayClient = new DefaultAlipayClient(certAlipayRequest);
        } catch (AlipayApiException e1) {
            e1.printStackTrace();
        }
        // 构造请求参数以调用接口
        AlipayOpenMiniVersionOnlineRequest   request = new AlipayOpenMiniVersionOnlineRequest();
        request.putOtherTextParam("app_auth_token", "202403BB0a199a29f36a494c88bee096f5d60X87");
        AlipayOpenMiniVersionOnlineModel model = new AlipayOpenMiniVersionOnlineModel ();

        // 设置商家小程序审核中的版本号
        model.setAppVersion("1.0.1");

        request.setBizModel(model);

        AlipayOpenMiniVersionOnlineResponse   response = alipayClient.certificateExecute(request);
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }

    //下架版本
    public static void offlineAppVersion() throws AlipayApiException {
        CertAlipayRequest certAlipayRequest = Tool.getRequestTemplate();
        AlipayClient alipayClient = null;
        try {
            alipayClient = new DefaultAlipayClient(certAlipayRequest);
        } catch (AlipayApiException e1) {
            e1.printStackTrace();
        }
        // 构造请求参数以调用接口
        AlipayOpenMiniVersionOfflineRequest    request = new AlipayOpenMiniVersionOfflineRequest ();
        request.putOtherTextParam("app_auth_token", "202403BB0a199a29f36a494c88bee096f5d60X87");
        AlipayOpenMiniVersionOfflineModel  model = new AlipayOpenMiniVersionOfflineModel();

        // 设置商家小程序审核中的版本号
        model.setAppVersion("1.0.1");
        // 设置小程序端
        model.setBundleId("com.alipay.alipaywallet");

        request.setBizModel(model);

        AlipayOpenMiniVersionOfflineResponse    response = alipayClient.certificateExecute(request);
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }


    //查询版本详情
    public static void queryAppVersionList() throws AlipayApiException {
        CertAlipayRequest certAlipayRequest = Tool.getRequestTemplate();
        AlipayClient alipayClient = null;
        try {
            alipayClient = new DefaultAlipayClient(certAlipayRequest);
        } catch (AlipayApiException e1) {
            e1.printStackTrace();
        }
        // 构造请求参数以调用接口
        AlipayOpenMiniVersionListQueryRequest   request = new AlipayOpenMiniVersionListQueryRequest();
        request.putOtherTextParam("app_auth_token", "202404BB326b531a912c441d8b5bb1d6b06cdX87");
        AlipayOpenMiniVersionListQueryModel    model = new AlipayOpenMiniVersionListQueryModel  ();

        // 设置版本状态列表
        model.setVersionStatus("AUDITING");

        // 设置端参数
        model.setBundleId("com.alipay.alipaywallet");

        request.setBizModel(model);

        AlipayOpenMiniVersionListQueryResponse   response = alipayClient.certificateExecute(request);
        System.out.println(JSON.parseObject(response.getBody()));
        JSONObject jsonObject = JSON.parseObject(response.getBody());
        String alipayOpenMiniVersionListQueryResponse = jsonObject.getString("alipay_open_mini_version_list_query_response");
        JSONObject parse = JSON.parseObject(alipayOpenMiniVersionListQueryResponse);

        System.out.println(parse.getJSONArray("app_versions"));
        JSONArray appVersions = parse.getJSONArray("app_versions");
        List<String> javaList = appVersions.toJavaList(String.class);
        System.out.println(javaList.get(javaList.size()-1));

        String appVersion = javaList.get(javaList.size()-1);
        if(StringUtils.isEmpty(appVersion)){
            appVersion = "0.0.1";
        }else{
            String[] split = appVersion.split("\\.");
            Integer s = Integer.parseInt(split[split.length - 1])+1;
            String newAppVersion = "";
            for (int i = 0; i < split.length; i++) {
                if(i==split.length-1){
                    newAppVersion = newAppVersion + s;
                }else{
                    newAppVersion = newAppVersion+split[i]+".";
                }
            }
            appVersion = newAppVersion;
        }
        System.out.println(appVersion);

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }

    //查询版本详情
    public static void queryAppVersionDetail() throws AlipayApiException {
        CertAlipayRequest certAlipayRequest = Tool.getRequestTemplate();
        AlipayClient alipayClient = null;
        try {
            alipayClient = new DefaultAlipayClient(certAlipayRequest);
        } catch (AlipayApiException e1) {
            e1.printStackTrace();
        }
        // 构造请求参数以调用接口
        AlipayOpenMiniVersionDetailQueryRequest    request = new AlipayOpenMiniVersionDetailQueryRequest ();
        request.putOtherTextParam("app_auth_token", "202404BB469afc9e389b4a7086bba78f906e7E11");
        AlipayOpenMiniVersionDetailQueryModel     model = new AlipayOpenMiniVersionDetailQueryModel   ();

// 设置小程序版本号
        model.setAppVersion("0.0.11");

        // 设置小程序端
        model.setBundleId("com.alipay.alipaywallet");

        request.setBizModel(model);

        AlipayOpenMiniVersionDetailQueryResponse  response = alipayClient.certificateExecute(request);
        System.out.println(JSON.parseObject(response.getBody()));
        JSONObject jsonObject = JSON.parseObject(response.getBody());
        String versionDetailQueryResponse = jsonObject.getString("alipay_open_mini_version_detail_query_response");
        JSONObject parse = JSON.parseObject(versionDetailQueryResponse);

        System.out.println(parse.getString("version_desc"));

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }

}
